const Login_url = process.env.VUE_APP_LOGIN_URL;

import { Message } from 'element-ui'

axios.defaults.withCredentials = true;

const service = axios.create({
  timeout: 5000
})

service.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    console.log(error);
    Promise.reject(error);
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.status === 200 || res.code === 200) {
      return res;
    } else if ((res.data && res.data.status === 400) || res.status === 401) {
      let url = window.location.href;
      window.location.href = Login_url + '?url=' + encodeURIComponent(url);
    } else {
      let msg = res.msg ? res.msg : res.statusInfo;
      Message({
        message: msg,
        type: 'error',
        duration: 5000
      });
      return Promise.reject('error');
    }
  },
  error => {
    Message({
      message: error,
      type: 'error',
      duration: 5000
    });
    return Promise.reject('error');
  }
)

export default service;
